package views;

import views.panels.PanelGeneral;
import logicBusiness.CursoBL;
import models.Curso;
import java.awt.BorderLayout;
import java.util.Collection;
import java.util.Iterator;

/**
 *
 * @author viviana
 */
public class InterfazAdministrarCurso extends javax.swing.JFrame {

    private int numeroCursosActuales = 0;

    /** Creates new form InterfazAdministrarCurso */
    public InterfazAdministrarCurso() {
        initComponents();
        PanelGeneral panel = new PanelGeneral();
        this.add(panel, BorderLayout.SOUTH);
        cursosExistentes();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    public void cursosExistentes(){
        String existentes = "El grado " + CBSeleccionarGrado.getSelectedItem() + "\ntiene estos cursos:";
        numeroCursosActuales = 0;
        CursoBL bo = new CursoBL();
        Collection<Curso> c = bo.encontrarCursosPorGrado(CBSeleccionarGrado.getSelectedIndex());
        Iterator<Curso> it = c.iterator();
        if (c.isEmpty()) {
            TPCursosActuales.setText("El grado " + CBSeleccionarGrado.getSelectedItem() + "\nno tiene cursos creados.");
        } else {
            while (it.hasNext()) {
//                existentes = existentes.concat("\n" + it.next().getNombreCurso());
            }
            TPCursosActuales.setText(existentes);
        }
        TPCursosAgregados.setText("");
    }
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        CBSeleccionarGrado = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        BAgregarCurso = new javax.swing.JButton();
        CBSelecCursoAgre = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        TPCursosActuales = new javax.swing.JTextPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        TPCursosAgregados = new javax.swing.JTextPane();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        CBSelecCursoEli = new javax.swing.JComboBox();
        BEliminarCurso = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        TPCursosEliminados = new javax.swing.JTextPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jLabel1.setText("Seleccione Un Grado:");

        CBSeleccionarGrado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0. Transición", "1. Primero", "2. Segundo", "3. Tercero", "4. Cuarto", "5. Quinto", "6. Sexto", "7. Septimo", "8. Octavo", "9. Noveno", "10. Decimo", "11. Once" }));
        CBSeleccionarGrado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBSeleccionarGradoActionPerformed(evt);
            }
        });

        jLabel2.setText("Cursos Actuales:");

        BAgregarCurso.setFont(new java.awt.Font("Tahoma", 1, 11));
        BAgregarCurso.setText("Agregar");
        BAgregarCurso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BAgregarCursoActionPerformed(evt);
            }
        });

        CBSelecCursoAgre.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1. Uno", "2. Dos", "3. Tres", "4. Cuatro", "5. Cinco", "6. Seis", "7.  Siete", "8. Ocho", "9. Nueve" }));
        CBSelecCursoAgre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBSelecCursoAgreActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setForeground(new java.awt.Color(0, 0, 240));
        jLabel3.setText("AGREGAR CURSOS");

        jLabel4.setText("Cuántos cursos desea agregar?:");

        TPCursosActuales.setBackground(new java.awt.Color(204, 204, 204));
        TPCursosActuales.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jScrollPane1.setViewportView(TPCursosActuales);

        TPCursosAgregados.setBackground(new java.awt.Color(204, 204, 204));
        TPCursosAgregados.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        TPCursosAgregados.setEditable(false);
        jScrollPane2.setViewportView(TPCursosAgregados);

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel5.setForeground(new java.awt.Color(0, 0, 240));
        jLabel5.setText("ELIMINAR CURSOS");

        jLabel6.setText("Escoja cuantos cursos desea eliminar: ");

        CBSelecCursoEli.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1. Uno", "2. Dos", "3. Tres", "4. Cuatro", "5. Cinco", "6. Seis", "7. Siete", "8. Ocho", "9. Nueve" }));
        CBSelecCursoEli.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBSelecCursoEliActionPerformed(evt);
            }
        });

        BEliminarCurso.setFont(new java.awt.Font("Tahoma", 1, 11));
        BEliminarCurso.setText("Eliminar");
        BEliminarCurso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BEliminarCursoActionPerformed(evt);
            }
        });

        TPCursosEliminados.setBackground(new java.awt.Color(204, 204, 204));
        TPCursosEliminados.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jScrollPane3.setViewportView(TPCursosEliminados);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(BAgregarCurso, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel5))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jLabel6)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(CBSelecCursoEli, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGap(3, 3, 3)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(jLabel4)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(CBSelecCursoAgre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel1)
                                            .addComponent(jLabel2)
                                            .addGroup(layout.createSequentialGroup()
                                                .addGap(16, 16, 16)
                                                .addComponent(BEliminarCurso, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                                                .addComponent(CBSeleccionarGrado, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE))))))))
                .addContainerGap(53, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jScrollPane1, jScrollPane2, jScrollPane3});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {CBSelecCursoAgre, CBSelecCursoEli});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CBSeleccionarGrado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(16, 16, 16)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(CBSelecCursoAgre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(53, 53, 53)
                        .addComponent(BAgregarCurso)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel5))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(57, 57, 57)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(CBSelecCursoEli, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(62, 62, 62)
                        .addComponent(BEliminarCurso)))
                .addGap(29, 29, 29))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jScrollPane1, jScrollPane2, jScrollPane3});

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {BAgregarCurso, BEliminarCurso});

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {CBSelecCursoAgre, CBSelecCursoEli});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BAgregarCursoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BAgregarCursoActionPerformed
        int grado = CBSeleccionarGrado.getSelectedIndex();
        int numeroCursos = CBSelecCursoAgre.getSelectedIndex();
        numeroCursosActuales = 0;
        if (!TPCursosAgregados.getText().equals("") && TPCursosAgregados.getText() != null) {
            CursoBL bo = new CursoBL();
            Collection<Curso> c = bo.encontrarCursosPorGrado(CBSeleccionarGrado.getSelectedIndex());
            Iterator<Curso> it = c.iterator();
            while (it.hasNext()) {
                numeroCursosActuales += 1;
                it.next();
            }
            if (grado == 0) {
                for (int i = 0; i <= numeroCursos; i++) {
                    Curso cu = bo.crearCurso(grado, "0" + String.valueOf(numeroCursosActuales + 1 + i));
                }
            } else {
                for (int i = 0; i <= numeroCursos; i++) {
                    Curso cu = bo.crearCurso(grado, String.valueOf(grado) + "0" + String.valueOf(numeroCursosActuales + 1 + i));
                }
            }

        } else {
            TPCursosAgregados.setText("Seleccione un número");
        }
    }//GEN-LAST:event_BAgregarCursoActionPerformed

    private void CBSelecCursoAgreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBSelecCursoAgreActionPerformed
        int grado = CBSeleccionarGrado.getSelectedIndex();
        int numeroCursos = CBSelecCursoAgre.getSelectedIndex();
        numeroCursosActuales = 0;
        CursoBL bo = new CursoBL();
        Collection<Curso> c = bo.encontrarCursosPorGrado(CBSeleccionarGrado.getSelectedIndex());
        Iterator<Curso> it = c.iterator();
        while (it.hasNext()) {
            numeroCursosActuales += 1;
            it.next();
        }
        String aCrear = "Se van a crear los cursos:";
        if (grado == 0) {
            for (int i = 0; i <= numeroCursos; i++) {
                aCrear = aCrear.concat("\n0" + String.valueOf(numeroCursosActuales + 1 + i));
            }
        } else {
            for (int i = 0; i <= numeroCursos; i++) {
                aCrear = aCrear.concat("\n" + String.valueOf(grado) + "0" + String.valueOf(numeroCursosActuales + 1 + i));
            }
        }

        TPCursosAgregados.setText(aCrear);
    }//GEN-LAST:event_CBSelecCursoAgreActionPerformed

    private void CBSeleccionarGradoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBSeleccionarGradoActionPerformed
        cursosExistentes();
    }//GEN-LAST:event_CBSeleccionarGradoActionPerformed

    private void BEliminarCursoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BEliminarCursoActionPerformed
        int grado = CBSeleccionarGrado.getSelectedIndex();
        int numeroCursos = CBSelecCursoEli.getSelectedIndex();
        numeroCursosActuales = 0;
        if (!TPCursosEliminados.getText().equals("") && TPCursosEliminados.getText() != null) {
            CursoBL bo = new CursoBL();
            Collection<Curso> c = bo.encontrarCursosPorGrado(CBSeleccionarGrado.getSelectedIndex());
            Iterator<Curso> it = c.iterator();
            while (it.hasNext()) {
                numeroCursosActuales += 1;
                it.next();
            }
            if ((numeroCursosActuales - numeroCursos) > 0) {
                if (grado == 0) {
                    for (int i = 0; i <= numeroCursos; i++) {
                        bo.borrarCurso("0" + String.valueOf(numeroCursosActuales - i));
                    }
                } else {
                    for (int i = 0; i <= numeroCursos; i++) {
                        bo.borrarCurso(String.valueOf(grado) + "0" + String.valueOf(numeroCursosActuales - i));
                    }
                }
            } else {
                TPCursosEliminados.setText("No puede borrar mas de los que hay");
            }
        } else {
            TPCursosEliminados.setText("Seleccione un número");
        }
    }//GEN-LAST:event_BEliminarCursoActionPerformed

    private void CBSelecCursoEliActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBSelecCursoEliActionPerformed
        int grado = CBSeleccionarGrado.getSelectedIndex();
        int numeroCursos = CBSelecCursoEli.getSelectedIndex();
        numeroCursosActuales = 0;
        CursoBL bo = new CursoBL();
        Collection<Curso> c = bo.encontrarCursosPorGrado(CBSeleccionarGrado.getSelectedIndex());
        Iterator<Curso> it = c.iterator();
        while (it.hasNext()) {
            numeroCursosActuales += 1;
            it.next();
        }

        if ((numeroCursosActuales - numeroCursos) > 0) {
            String aCrear = "Se van a eliminar los cursos:";
            if (grado == 0) {
                for (int i = 0; i <= numeroCursos; i++) {
                    aCrear = aCrear.concat("\n0" + String.valueOf(numeroCursosActuales - i));
                }
            } else {
                for (int i = 0; i <= numeroCursos; i++) {
                    aCrear = aCrear.concat("\n" + String.valueOf(grado) + "0" + String.valueOf(numeroCursosActuales - i));
                }
            }

            TPCursosEliminados.setText(aCrear);
        } else {
            TPCursosEliminados.setText("No puede borrar mas de los que hay");
        }

    }//GEN-LAST:event_CBSelecCursoEliActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new InterfazAdministrarCurso().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BAgregarCurso;
    private javax.swing.JButton BEliminarCurso;
    private javax.swing.JComboBox CBSelecCursoAgre;
    private javax.swing.JComboBox CBSelecCursoEli;
    private javax.swing.JComboBox CBSeleccionarGrado;
    private javax.swing.JTextPane TPCursosActuales;
    private javax.swing.JTextPane TPCursosAgregados;
    private javax.swing.JTextPane TPCursosEliminados;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    // End of variables declaration//GEN-END:variables
}
